﻿/******************************************************************************
* File description: AnimationLayout2.qml
* Function: 打靶动画模块（包含摄像头、 按钮等布局）
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.12

import "../components"
import "../pageComponents"
import "../animation/practiceAnimation"
import "../camera"

Rectangle{
    id: root
    height: parent.height
    width: parent.width
    radius: 5*dp
    clip: true
    color: "#E0E6E9"

    property int showMode: 0        // 0放大显示动画 1放大显示摄像头
    property bool editable: (gExpIsRunning===0 || gExpIsFailure&1==1) && gConnectStatus===true

    // 右上角的尺码
    property real graphWidth: Math.max(Math.min(width, height*1.5)*0.17 * 1.3, 200*dp)
    property real graphHeight: Math.max(Math.min(width, height*1.5)*0.17, 140*dp)

    property real boxPadding: 20*dp

    signal fullScreen()

    Text{
        x: 20*dp; y: 20*dp
        font.bold: true
        text: qsTr("实验流程")
        font.pixelSize: config.fontsize_1
        z: 100
    }


    onVisibleChanged: {
        if(visible){
            canvas_practice.startAnimation()
        }else{
            canvas_practice.stopAnimation()
        }
    }
    Item{
        id: fakeCanvasAnchors       // 图画小了，改了一下画布的尺码
        height: root.height * 1.2
        width: root.width * 1.3
        x: - root.width * 0.15
    }
    PracticeCanvas{
        id: canvas_practice
        anchors.fill: showMode === 0 ? fakeCanvasAnchors : area_corner
        z: showMode
    }

    CameraDisplay{
        cameraName: "CameraPractice"
        cameraId: gStateCamera.cameraId_2
        anchors.fill: showMode === 1 ? root : area_corner
        z: 1-showMode
    }
    MouseArea{
        id: area_corner
        height: graphHeight; width: graphWidth
        y: boxPadding; x: parent.width - width - boxPadding
        cursorShape: Qt.PointingHandCursor
        hoverEnabled: true
        onEntered: { toolTip_doubleClicked.visible = true }
        onExited: { toolTip_doubleClicked.visible = false }
        onDoubleClicked: {
            showMode = 1 - showMode
        }
        CustomTooltip{
            id: toolTip_doubleClicked
            text: "双击放大显示"
        }
    }
    Column{
        x: area_corner.x
        y: area_corner.y + area_corner.height + 8*dp
        width: 210*dp
        spacing: 2*dp
        // 暂停
        // 暂停
        TimerCard_pause{
            id: pauseModule
            z: 100
            width: area_corner.width
        }
        // 等待
        TimerCard_wait{
            id: waitModule
            z: 100
            width: area_corner.width
        }
        // 气泵
        TimerCard_air{
            id: timerCard_air
            width: area_corner.width
        }
        // 水泵
        TimerCard_water{
            id: timerCard_water
            width: area_corner.width
        }
        TimerCard_failure{
            id: timerCard_failure
            width: area_corner.width
            onClicked: {
                box_failureInfo2.show()
            }
        }
    }

    // 右下角的按钮区域
    Column{
        spacing: 16*dp
        x: parent.width - width - 20*dp
        y: parent.height - height - 20*dp
        CircleButton{       // 全屏
            icon: "qrc:/picture/animation/fullScreen.png"
            icon_hover: "qrc:/picture/animation/fullScreen_h.png"
            onClicked: {
                root.fullScreen()
            }
            CustomTooltip{
                text: "全屏"
                visible: parent.isHovered
            }
        }
        CircleButton{       // 按钮
            id: button_showButtonList
            icon: "qrc:/picture/animation/showButtonList.png"
            icon_hover: "qrc:/picture/animation/showButtonList_h.png"
            property bool showButtonList: false
            onClicked: {
                showButtonList = !showButtonList
            }
            CustomTooltip{
                text: "操作按钮栏"
                visible: parent.isHovered
            }
        }
        z: 1
    }
    Item{    // 可收缩式按钮
        id: layout_button
        x: 40*dp;   width: parent.width - 2*x
        y: parent.height - 20*dp - height;     height: 50*dp
        clip: true
        Rectangle{
            id: button_area
            radius: 25*dp
            height: 50*dp
            width: row_button.width + 120*dp
            state: button_showButtonList.showButtonList ? "show" : "hide"
            states: [
                State {
                    name: "show"
                    PropertyChanges {
                        target: button_area
                        x: layout_button.width- width + 40*dp
                        opacity: 1
                    }
                },
                State {
                    name: "hide"
                    PropertyChanges {
                        target: button_area
                        x: layout_button.width
                        opacity: 0
                    }
                }
            ]
            transitions: [
                Transition {
                    from: "show"
                    to: "hide"
                    NumberAnimation{
                        properties: "x"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }NumberAnimation{
                        properties: "opacity"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                },
                Transition {
                    from: "hide"
                    to: "show"
                    NumberAnimation{
                        properties: "x"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }NumberAnimation{
                        properties: "opacity"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                }
            ]
            Row{
                id: row_button
                anchors.verticalCenter: parent.verticalCenter
                x: 25*dp
                height: 36*dp
                spacing: 8*dp

                CustomButton{
                    id: button_blowTarget
                    title: "加速端回靶"
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    defaultMode1: true
                    onClicked: {
                        gStateOp.openPopup("28", [])
                        CommunicationMethod.stepOperatingSimulation("2","7",["加速端回靶"])
                        CAnimationPractice.openBlowTarget()
                    }
                }
//                CustomButton{
//                    id: button_waterPump
//                    title: "冷却"
//                    autoWidth: true
//                    height: 36*dp
//                    enabled: root.editable
//                    defaultMode1: true
//                    onClicked: {
//                        box_waterAir.showWaterPump()
//                    }
//                }
//                CustomButton{
//                    id: button_airPump
//                    title: "干燥"
//                    autoWidth: true
//                    height: 36*dp
//                    enabled: root.editable
//                    defaultMode1: true
//                    onClicked: {
//                        box_waterAir.showAirPump()
//                    }
//                }
                CustomButton{
                    id: button_lock1
                    title: "抱闸状态-锁止"
                    enabled: root.editable
                    defaultMode1: true
                    autoWidth: true
                    height: 36*dp
                    onClicked: {
                        gStateOp.openPopup("26", [])
                        CommunicationMethod.stepOperatingSimulation("2","5",["锁止"])
                        CAnimationPractice.openBrake()
                    }
                }
                CustomButton{
                    id: button_lock2
                    title: "抱闸状态-松开"
                    enabled: root.editable
                    defaultMode1: true
                    autoWidth: true
                    height: 36*dp
                    onClicked: {
                        gStateOp.openPopup("26", [])
                        CommunicationMethod.stepOperatingSimulation("2","5",["松开"])
                        CAnimationPractice.closeBrake()
                    }
                }
                CustomButton{
                    id: button_moveTarget1
                    title: "退靶"
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    defaultMode1: true
                    onClicked: {
                        gStateOp.openPopup("25", [])
                        CommunicationMethod.stepOperatingSimulation("2","4",["退靶"])
                        CAnimationPractice.barMoveRight()
                        timer_jieBa.start()
                    }
                    Timer{
                        id: timer_jieBa
                        interval: 1000
                        onTriggered: {
                            CAnimationPractice.turnWheelDown()
                        }
                    }
                }
                CustomButton{
                    id: button_moveTarget2
                    title: "进靶"
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    defaultMode1: true
                    onClicked: {
                        gStateOp.openPopup("25", [])
                        CommunicationMethod.stepOperatingSimulation("2","4",["进靶"])
                        CAnimationPractice.turnWheelLeft()
                        timer_songBa.start()
                    }
                    Timer{
                        id: timer_songBa
                        interval: 1000
                        onTriggered: {
                            CAnimationPractice.barMoveLeft()
                        }
                    }
                }
                CustomButton{
                    id: button_suction1
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    title: "吸取梭体-开"
                    defaultMode1: true
                    onClicked: {
                        gStateOp.openPopup("22", [])
                        CAnimationPractice.openAbsorb()
                        CommunicationMethod.stepOperatingSimulation("2","1",["开启"])
                    }
                }
                CustomButton{
                    id: button_suction2
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    title: "吸取梭体-关"
                    defaultMode1: true
                    onClicked: {
                        gStateOp.openPopup("22", [])
                        CAnimationPractice.closeAbsorb()
                        CommunicationMethod.stepOperatingSimulation("2","1",["关闭"])
                    }
                }
                CustomButton{
                    id: button_feedPort1
                    title: "梭体上靶-上移"
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    defaultMode1: true
                    onClicked: {
                        gStateOp.openPopup("27", [])
                        CommunicationMethod.stepOperatingSimulation("2","6",["上移"])
                        CAnimationPractice.feedPortUp()
                    }
                }
                CustomButton{
                    id: button_feedPort2
                    title: "梭体上靶-下移"
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    defaultMode1: true
                    onClicked: {
                        gStateOp.openPopup("27", [])
                        CommunicationMethod.stepOperatingSimulation("2","6",["下移"])
                        CAnimationPractice.feedPortDown()
                    }
                }
            }
        }
    }

}
